/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.services.vtutoribm;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.hibernate.Session;
import ru.miit.elearning.hibernate.dao.ConfigurationDAO;
import ru.miit.elearning.hibernate.dao.CourseDAO;
import ru.miit.elearning.hibernate.dao.CurriculumDAO;
import ru.miit.elearning.hibernate.dao.HibernateUtil;
import ru.miit.elearning.hibernate.dto.CurriculumLevel;
import ru.miit.elearning.hibernate.dto.UserIdentity;
import ru.miit.elearning.hibernate.dto.WorkItem;

/**
 *
 * @author Cyclone
 */
@WebService()
public class VirtualTutorIBM {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getZosCheckBundle")
    public ZosCheckBundle getZosCheckBundle(@WebParam(name = "courseId") int courseId, @WebParam(name = "tutorLogin") String tutorLogin) throws VirtualTutorIBMServiceException {
        ZosCheckBundle checkBundle = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            checkBundle = new ZosCheckBundle();
            checkBundle.setCourseId(courseId);

            CurriculumDAO curriculumDAO = new CurriculumDAO(session);

            if (!curriculumDAO.isTutorSignedOnCourse(curriculumDAO.findUserIdByLogin(tutorLogin), courseId)) {
                // TODO tutor not signed on course
                System.err.println("\tVirtualTutorIBM: Tutor "+tutorLogin+" not signed on course ID"+courseId);
                return null;
            }
            CourseDAO courseDAO = new CourseDAO(session);
            CurriculumLevel course = courseDAO.findCourse(courseId);
            if (course == null) {
                // no such course
                // VirtualTutorIBMServiceException
                System.out.println("\tVirtualTutorIBM: No such course ID"+courseId);
                return null;
            }

            Mapper dozerMapper = DozerBeanMapperSingletonWrapper.getInstance();

            List<CurriculumLevel> curriculumList = course.getLabList();
            List<VlLab> labList = new ArrayList<VlLab>(curriculumList.size());
            for (CurriculumLevel curriculumLevel : curriculumList) {
                labList.add(
                        dozerMapper.map(curriculumLevel, VlLab.class));
            }
            checkBundle.setLabList(labList);

            ConfigurationDAO configurationDAO = new ConfigurationDAO(session);

            List<UserIdentity> uiList = courseDAO.findStudentsByCourse(courseId);
            List<VlStudent> studentList = new ArrayList<VlStudent>();

            for (UserIdentity ui : uiList) {
                VlStudent student = dozerMapper.map(ui, VlStudent.class);

                student.setZOSHomeFolder(configurationDAO.findUserPropertyValueByAlias(student.getId(), ConfigurationDAO.ALIAS_ZOS_HOME_FOLDER));
                studentList.add(student);
            }
            checkBundle.setStudentList(studentList);


            //CurriculumDAO curriculumDAO = new CurriculumDAO(session);
            List<WorkItem> approvedList = curriculumDAO.findApproved(curriculumList, uiList);
            Set<VlApprovedLab> approvedSet = new HashSet<VlApprovedLab>(approvedList.size());
            for (WorkItem wi : approvedList) {
                approvedSet.add(
                        dozerMapper.map(wi, VlApprovedLab.class));
            }
            checkBundle.setApprovedSet(approvedSet);

            checkBundle.setFtpFullUrl(
                    configurationDAO.findCurriculumPropertyValueByAlias(courseId, ConfigurationDAO.ALIAS_VIRTUAL_TUTOR_IBM)
                    );

            if (session != null && session.isOpen()) {
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return checkBundle;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getZosFtpServerHostByCourse")
    public String getZosFtpServerHostByCourse(@WebParam(name = "courseId") int courseId) {
        //TODO write your implementation code here:
        String ftpServerHost = null;

        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            ConfigurationDAO configurationDAO = new ConfigurationDAO(session);
            ftpServerHost = configurationDAO.findCurriculumPropertyValueByAlias(courseId, ConfigurationDAO.ALIAS_VIRTUAL_TUTOR_IBM);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }

        return ftpServerHost;
    }
    /**
     * Web service operation
     */
    /*
    @WebMethod(operationName = "sayHello")
    public String sayHello(@WebParam(name = "name")
    String name) {
    //TODO write your implementation code here:
    System.out.println("\t=== here");
    return String.format("Hello, %s!", name);
    //return "Hello, "+name+"!";
    }
     */
}
